var server = require('../../../utils/server');
var WxParse = require('../../../wxParse/wxParse.js');
var objectId;
Page({
	data: {
		goods: {},
		current: 0,
		tabStates:[true,false,false],
		tabClasss:["text-select","text-normal","text-normal"],
		galleryHeight: getApp().screenWidth,
		tab:0,
		goods_num:1,
		textStates:["view-btns-text-normal","view-btns-text-select"],
        good_info:[],
        num:1,
	},
	onLoad: function(options) {
        // server.postJSON('/cart/goodsinfo', { goods_id: options.id }, res => {
        //     if(res.data.status==200){
        //         // res.data.list.content = getApp().convertHtmlToText(res.data.list.content);
        //         var that = this;
        //         WxParse.wxParse('article', 'html', res.data.list.content, that, 5);
        //         this.setData({
        //             goods: res.data.list
        //         })

        //     }
        // })
    server.send('/cart/goodsinfo', 'POST', { goods_id: options.id }).then(res=>{
      if (res.status == 200) {
                // res.data.list.content = getApp().convertHtmlToText(res.data.list.content);
                var that = this;
                WxParse.wxParse('article', 'html', res.list.content, that, 5);
                this.setData({
                    goods: res.list
                })

            }
    });
        // server.postJSON('/cart/spec', { goods_id: options.id }, res => {
        //     if (res.data.status == 200) {
        //         var good_info = res.data.list;
        //         for (var i = 0; i < good_info.length; i++) {
        //             for (var a = 0; a < good_info[i].data.length; a++) {
        //                 if (a == 0) {
        //                     good_info[i].data[a].isClick = 1;
        //                 } else {
        //                     good_info[i].data[a].isClick = 0;
        //                 }
        //             }
        //         }
        //         this.setData({
        //             good_info: good_info
        //         })
                
        //     }
        // })
    server.send('/cart/spec', 'POST', { goods_id: options.id}).then(res=>{
      if (res.status == 200) {
                var good_info = res.list;
                for (var i = 0; i < good_info.length; i++) {
                    for (var a = 0; a < good_info[i].data.length; a++) {
                        if (a == 0) {
                            good_info[i].data[a].isClick = 1;
                        } else {
                            good_info[i].data[a].isClick = 0;
                        }
                    }
                }
                this.setData({
                    good_info: good_info
                })

            }
    })
        // this.regoodinfo(options.id);
		// var goodsId = options.objectId;
		// objectId = goodsId;
		// this.getGoodsById(goodsId);
        // server.postJSON('/cart/get_comment', { goods_id: options.id }, res => {
        //     if (res.data.status == 200) {
        //         // res.data.list.content = getApp().convertHtmlToText(res.data.list.content);
        //     }
        // })
    server.send('/cart/get_comment', 'POST', { goods_id: options.id }).then(res=>{
      if (res.status == 200) {
            //         // res.data.list.content = getApp().convertHtmlToText(res.data.list.content);
                }
        })
	},
    onShow(){
        this.regoodinfo();
    },
	tabClick:function(e){
        var index = e.currentTarget.dataset.index
		var classs= ["text-normal","text-normal","text-normal"]
		classs[index] = "text-select"
		this.setData({tabClasss:classs,tab:index})
	},
	
	checkPrice:function(){
		var goods = this.data.goods;
        var spec = ""
	    this.setData({price:goods.goods.shop_price});
        for(var i= 0 ; i < goods.goods.goods_spec_list.length;i++)
		{
              
			  for(var j= 0 ; j < goods.goods.goods_spec_list[i].length;j++)
			  {
				  if(goods.goods.goods_spec_list[i][j].isClick == 1)
				  {
                    if(spec == "")
                    spec = goods.goods.goods_spec_list[i][j].item_id
                    else
                    spec = spec + "_" + goods.goods.goods_spec_list[i][j].item_id
				  }
			  }
		}
        
		var specs = spec.split("_");
		for(var i = 0 ; i < specs.length;i++)
		{
           specs[i] = parseInt(specs[i])
		}
        specs.sort(function(a,b){return a - b});
		spec = ""
		for(var i = 0 ; i < specs.length;i++)
		{
            if(spec == "")
            spec = specs[i]
            else
            spec = spec + "_" + specs[i]
		}
		var price = goods['spec_goods_price'][spec].price;
		this.setData({price:price});
	},
	addCart: function() {

		var goods = this.data.goods;
		var spec = ""
		if(goods.goods.goods_spec_list !=null)
        for(var i= 0 ; i < goods.goods.goods_spec_list.length;i++)
		{
              
			  for(var j= 0 ; j < goods.goods.goods_spec_list[i].length;j++)
			  {
				  if(goods.goods.goods_spec_list[i][j].isClick == 1){
                        if(spec == "")
                        spec = goods.goods.goods_spec_list[i][j].item_id
                        else
                        spec = spec + "_" + goods.goods.goods_spec_list[i][j].item_id
				  }
			  }
		}
		var app = getApp()
		var that = this;
        var goods_id = that.data.goods.goods.goods_id;
        var goods_spec = spec;
		var session_id = app.globalData.openid//that.data.goods.goods.spec_goods_price
		var goods_num = that.data.goods_num;
		var user_id = "0"
        if(app.globalData.login){
		    user_id = app.globalData.userInfo.user_id
            // server.getJSON('/Cart/addCart',{goods_id:goods_id,goods_spec:goods_spec,session_id:session_id,goods_num:goods_num,user_id:user_id},function(res){
            // if(res.data.status == 1)
            //     wx.showToast({
            //         title: '已加入购物车',
            //         icon: 'success',
            //         duration: 800
            //     });
            //     else
            //     wx.showToast({
            //         title: res.data.msg,
            //         icon: 'error',
            //         duration: 800
            //     });
            // });
          server.send('/Cart/addCart','POST',{
            goods_id: goods_id,
             goods_spec: goods_spec, 
             session_id: session_id, 
             goods_num: goods_num, 
             user_id: user_id
          }).then(res=>{
            if (res.status == 1)
                wx.showToast({
                    title: '已加入购物车',
                    icon: 'success',
                    duration: 800
                });
                else
                wx.showToast({
                    title: res.msg,
                    icon: 'error',
                    duration: 800
                });
          })
        }
	},
	showCartToast: function () {
		wx.showToast({
			title: '已加入购物车',
			icon: 'success',
			duration: 800
		});
		wx.navigateTo({
			url: '../../../../../../cart/cart'
		});

	},
	previewImage: function (e) {
		wx.previewImage({
			//从<image>的data-current取到current，得到String类型的url路径
			current: this.data.goods.get('images')[parseInt(e.currentTarget.dataset.current)],
			urls: this.data.goods.get('images') // 需要预览的图片http链接列表
		})
	},
    regoodinfo(id){
        
    },
    rebtn(){
        var good_info = this.dataset.good_info;
        for (var i = 0; i < good_info.length; i++) {
            for (var a = 0; a < good_info[i].data.length; a++) {
                    good_info[i].data[a].isClick = 0;
            }
        }
        this.setData({
            good_info:good_info
        })
    },
    btnclick(e){
        // var proindex = e.currentTarget.dataset.pro;
        // var index = e.currentTarget.dataset.index;
        // var id = e.currentTarget.dataset.id;
        // var spec_id = e.currentTarget.dataset.spec_id;
        var data = e.currentTarget.dataset;
        var good_info = this.data.good_info;
        for (var b = 0; b < good_info[data.pos].data.length; b++) {
            good_info[data.pos].data[b].isClick = 0;
                
        }
        good_info[data.pos].data[data.index].isClick = 1;
        this.setData({
            good_info:good_info
        })
       
    },
    bindMinus(){
        var num = this.data.num;
        if (num > 1) {
            num--;
        }
        this.setData({num: num });
    },
    bindPlus(){
        this.setData({
            num: this.data.num + 1
        })
    },
    bindManual(e){
        this.setData({
            num:e.detail.value*1
        })
    },
    addCart(){
        let dtl=[];
        let good_info = this.data.good_info;
        for(let a=0;a<good_info.length;a++){
            for(let b=0;b<good_info[a].data.length;b++){
                if(good_info[a].data[b].isClick){
                    dtl.push( good_info[a].data[b].id)
                }
            }
        }
        let _params={
            goods_id: this.data.goods.goods_id, 
            spec_key: dtl.join('_'), 
            goods_num: this.data.num,
            username:getApp().globalData.username,
            token: getApp().globalData.token,
        }
        
        // server.postJSON('/cart/cart1', _params,res=>{
        //     if(res.data.status==200){
        //         wx.showToast({
        //             title:'成功',
        //             icon: 'succes',
        //             duration: 800,
        //             mask: true
        //         })
        //     }else{
        //         wx.showToast({
        //             title: '请先登录',
        //             icon: 'none'
        //         })
        //     }
        // })
      server.send('/cart/cart1','POST',_params).then(res=>{
        if (res.status == 200) {
                wx.showToast({
                    title:'成功',
                    icon: 'succes',
                    duration: 800,
                    mask: true
                })
            }else{
                wx.showToast({
                    title: '请先登录',
                    icon: 'none'
                })
            }
      })
        wx.setStorageSync('token',_params.token);
        wx.setStorageSync('username', _params.username);
    },
    buy(){
        this.addCart();
        wx.switchTab({
            url: '../../cart/cart',
        })
        // setTimeout(()=>{
        //     try{
                
        //     }catch(error){
        //     }
        // },1000)
    },
    
});